using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace SBS_Map_Editor.Components
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Editor : DrawableGameComponent
    {
        #region Variables
        public static short SplashDelay = 3;

        private Form splash;
        private MainForm editor;

        private SpriteBatch batch;
        private Texture2D splashScreen;
        #endregion

        #region Constructor
        public Editor(Game game)
            : base(game)
        {
            splash = Form.FromHandle(game.Window.Handle) as Form;
            splash.FormBorderStyle = FormBorderStyle.None;
        }
        #endregion

        #region XNA Functions
        #region Initialize
        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            SplashDelay *= 60;

            base.Initialize();
        }
        #endregion

        #region Cotent
        protected override void LoadContent()
        {
            batch = new SpriteBatch(GraphicsDevice);
            splashScreen = Game.Content.Load<Texture2D>("Splash");

            base.LoadContent();
        }
        #endregion

        #region Update
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            if (splash.Visible)
            {
                if (SplashDelay == 0)
                {
                    splash.Visible = false;

                    editor = new MainForm();
                    editor.HandleDestroyed += new EventHandler(HandleDestroyed);
                    editor.Show();
                }
                else
                    SplashDelay--;
            }

            base.Update(gameTime);
        }
        #endregion

        #region Draw
        public override void Draw(GameTime gameTime)
        {
            if (splash.Visible)
            {
                batch.Begin();
                batch.Draw(splashScreen, Vector2.Zero, Color.White);
                batch.End();
            }

            base.Draw(gameTime);

            if (editor != null)
                GraphicsDevice.Present(editor.ViewportHandle);
        }
        #endregion
        #endregion

        #region Helper Functions
        private void HandleDestroyed(object sender, EventArgs e)
        {
            Game.Exit();
        }
        #endregion
    }
}